package com.haoshuang.zhouzhoubang.ui.page.data.data

import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.FragmentDataStatisticsBinding
import com.haoshuang.zhouzhoubang.model.bean.DataStatisticalBean
import com.haoshuang.zhouzhoubang.ui.dialog.ChooseDateRangeDialog
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visible
import com.mufeng.libs.utils.view.visibleOrGone

/**
 *  
 */
class DataStatisticsFragment : BaseVMFragment<DataStatisticsViewModel, FragmentDataStatisticsBinding>() {

    companion object {
        fun getInstance(): DataStatisticsFragment {
            val fragment = DataStatisticsFragment()
            fragment.arguments = Bundle().apply {

            }
            return fragment
        }
    }

    /**
     * 1: 今天, 2: 本周, 3: 本月, 4: 本年 0: 自定义
     */
    private var type = 1

    private var start_time = ""
    private var end_time = ""

    override fun initView(savedInstanceState: Bundle?) {

        binding.rgTime.setOnCheckedChangeListener { group, checkedId ->
            binding.tvCustomTime.gone()
            start_time = ""
            end_time = ""
            when (checkedId) {
                binding.rbToday.id -> {
                    if (binding.rbToday.isChecked) {
                        type = 1
                        vm.getDataStatistical(type, start_time, end_time)
                    }
                }
                binding.rbCurrentWeek.id -> {
                    if (binding.rbCurrentWeek.isChecked) {
                        type = 2
                        vm.getDataStatistical(type, start_time, end_time)
                    }
                }
                binding.rbCurrentMonth.id -> {
                    if (binding.rbCurrentMonth.isChecked) {
                        type = 3
                        vm.getDataStatistical(type, start_time, end_time)
                    }
                }
                binding.rbCurrentYear.id -> {
                    binding.tvCustomTime.gone()
                    if (binding.rbCurrentYear.isChecked) {
                        type = 4
                        vm.getDataStatistical(type, start_time, end_time)
                    }
                }
                binding.rbCustomTime.id -> {
                    if (binding.rbCustomTime.isChecked) {
                        type = 0
                        start_time = ""
                        end_time = ""
                        binding.tvCustomTime.visible()
                        binding.tvCustomTime.text = "请选择查询时间段"
                        XPopup.Builder(mActivity)
                            .asCustom(ChooseDateRangeDialog(mActivity){start_date, end_date ->
                                start_time = start_date
                                end_time = end_date
                                binding.tvCustomTime.text = start_date + " ~ " + end_date
                                vm.getDataStatistical(type, start_time, end_time)
                            })
                            .show()
                    }else{
                        binding.tvCustomTime.gone()
                    }
                }
            }
        }

        binding.tvCustomTime.click {
            XPopup.Builder(mActivity)
                .asCustom(ChooseDateRangeDialog(mActivity){start_date, end_date ->
                    start_time = start_date
                    end_time = end_date
                    binding.tvCustomTime.text = start_date + " ~ " + end_date
                    vm.getDataStatistical(type, start_time, end_time)
                })
                .show()
        }

    }

    override fun initData() {

        vm.getDataStatistical(type, start_time, end_time)

    }

    override fun lazyLoadData() {

    }

    override fun startObserve() {

        vm.getDataStatisticalData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                setupData(it?.data)
            }
        }

    }

    private fun setupData(data: DataStatisticalBean?) {
        if (data == null) {
            binding.tvAccumulatedIncomeMoney.text = "0.0"
            binding.tvFreezeAmountMoney.text = "0.0"
            binding.tvStoreRevenueMoney.text = "0.0"
            binding.tvPlatformPushMoney.text = "0"
            binding.tvServiceOrdersMoney.text = "0"
            binding.tvTeamMemberIncomeMoney.text = "0.0"
        }else{
            binding.tvAccumulatedIncomeMoney.text = data.total_income
            binding.tvFreezeAmountMoney.text = data.freeze_money
            binding.tvStoreRevenueMoney.text = data.order_total_amount
            binding.tvPlatformPushMoney.text = data.tui_order_num
            binding.tvServiceOrdersMoney.text = data.order_total
            binding.tvTeamMemberIncomeMoney.text = data.child_total_income
        }
    }

}